﻿using SpiderLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PostTools
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            cobMethond.SelectedIndex = 0;
        }

        HttpHelper urlHelper = new HttpHelper();
        private void btngo_Click(object sender, EventArgs e)
        {
            HttpItem httpItem = new HttpItem();
            httpItem.URL = txtUrl.Text.Trim();
            httpItem.Method = cobMethond.SelectedItem.ToString();
            httpItem.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
            if (cobMethond.SelectedItem.ToString() == "POST")
            {
                httpItem.Postdata = txtPostData.Text;
            }
            HttpResult res= urlHelper.GetHtml(httpItem);

            //txtResult.Text = GetPostString(httpItem.URL, httpItem.Postdata);
            txtResult.Text = res.Html;
            webBrowser1.DocumentText = res.Html;

        }


        public string GetPostString(string url, string data)
        {
            try
            {
                byte[] postBytes = Encoding.GetEncoding("utf-8").GetBytes(data);
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
                myRequest.Method = "POST";
                myRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8 ";
                myRequest.ContentLength = postBytes.Length;
                myRequest.Proxy = null;
                Stream newStream = myRequest.GetRequestStream();
                newStream.Write(postBytes, 0, postBytes.Length);
                newStream.Close();
                // Get response
                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                using (StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.GetEncoding("utf-8")))
                {
                    string content = reader.ReadToEnd();
                    return content;
                }
            }
            catch (System.Exception ex)
            {
                return ex.Message;
            }
        }

        private void btntoLow_Click(object sender, EventArgs e)
        {
            txtPostData.Text=txtPostData.Text.ToLower();
        }

      
    }
}
